package cn.itsource._02_sync;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {
    public static void main(String[] args) throws Exception{
        // 1.创建消费者对象
        DefaultMQProducer producer = new DefaultMQProducer("producer_sync_group");
        // 2.连接nameServer
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 3.启动消费者
        producer.start();
        // 4.发送异步消息
        Message message = new Message("topic", "sync", "我在源码学习Java！Java简单易学！".getBytes(StandardCharsets.UTF_8));
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);

            }

            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("发送消息失败！");
            }
        });
        // 单向发送
        //producer.sendOneway(message);
    }
}
